<?php

namespace Controller\Passport;

class ResetPassword extends \Controller\Base {

	public function actionIndex() {
		$this->setMeta('重置密码');
		$time = $this->getParam('time');
		$encodeUid = $this->getParam('uid');
		$token = $this->getParam('token');
		\Business\User::instance()->checkUserResetPassword($time, $encodeUid, $token);
		if($this->isPost()){
			$this->updatePassword($encodeUid, $token);
		}
		$this->display();
	}

	/**
	 * 修改密码
	 *
	 * @param $encodeUid Uid
	 *
	 * @param $token
	 *
	 * @return string
	 */
	private function updatePassword($encodeUid, $token){
		$password = $this->postParam('password');
		$register = $this->postParam('register');
		$uid = \Business\User::instance()->getUid($encodeUid, $token);
		if($password == $register && $password){
			\Business\User::instance()->updatePasswordById($uid, $password);
			$url = $_SERVER['HTTP_ORIGIN'] . '/login.html';
			\Lib\Util\Comm::instance()->redirect($url);
		}
	}
}